package Model;

import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Iterator;

import org.json.JSONObject;

import android.graphics.Bitmap;

public class Restaurant {
	final long restaurantID;
	final String restaurantName;
	final String phoneNumber;
	final String address;
	final Date openHour;
	final Date closeHour;
	final Boolean currentAvailable;
	
	final String logoAddress;
	
	final Bitmap restaurantLogo () {
		return null;
	}
	
	final Array listOfPicture () {
		return null;
	}
	
	public Restaurant (long rid) {
		restaurantID = rid;
		String _restaurantName = null, _phoneNumber = null, _address = null, _openHour = null, _closeHour = null, _logoAddr = null; Boolean _currentAvailable = false;
		try {
			URL url = new URL("http://happywaiman.dlinkddns.com/Reserve/Company/Info.php?id="+rid);
			ReadFromURL reader = new ReadFromURL();
			String content = reader.getContentFromURL(url);
			JSONObject obj = new JSONObject(content);
			@SuppressWarnings("unchecked")
			Iterator<String> it = obj.keys();
			while (it.hasNext()) {
				String key = it.next();
				if ("CompanyName".equalsIgnoreCase(key)) {
					_restaurantName = obj.getString(key);
				} else if ("phoneNumber".equalsIgnoreCase(key)) {
					_phoneNumber = obj.getString(key);
				} else if ("StoreAddr".equalsIgnoreCase(key)) {
					_address = obj.getString(key);
				} else if ("OpenHour".equalsIgnoreCase(key)) {
					_openHour = obj.getString(key);
				} else if ("CloseHour".equalsIgnoreCase(key)) {
					_closeHour = obj.getString(key);
				} else if ("CurrentlyAvailable".equalsIgnoreCase(key)) {
					_currentAvailable = obj.getBoolean(key);
				} else if ("LogoAddress".equalsIgnoreCase(key)) {
					_logoAddr = obj.getString(key);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		restaurantName = _restaurantName;
		phoneNumber = _phoneNumber;
		address = _address;
		DateFormat formatter = DateFormat.getTimeInstance();
		Date tmp = null;
		try {
			tmp = formatter.parse(_openHour);
		} catch (Exception e) {
			e.printStackTrace();
		}
		openHour = tmp;
		tmp = null;
		try {
			tmp = formatter.parse(_closeHour);
		} catch (Exception e) {
			e.printStackTrace();
		}
		closeHour = tmp;
		currentAvailable = _currentAvailable;
		logoAddress = _logoAddr;
		
	}
}
